package cn.afactory.tygz.ui.base.adapter;

import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.harwkin.nb.camera.BaseImgView;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;

import java.util.ArrayList;
import java.util.List;

import cn.afactory.tygz.R;
import cn.afactory.tygz.model.UploadFileInfo;
import cn.afactory.tygz.ui.base.utils.ImageUtils;

/**
 * Created by Kenneth on 15/9/17.
 */
public class UserProfileImgGridAdapter extends RecyclerView.Adapter<UserProfileImgGridAdapter.UserProfileImgGridViewHolder> {

    private List<UploadFileInfo> listData = new ArrayList<UploadFileInfo>();
    private MyItemClickListener itemClickListener;

    public UserProfileImgGridAdapter(List<UploadFileInfo> listData) {
        this.listData = listData;
    }

    public List<UploadFileInfo> getListData() {
        return listData;
    }

    public void setListData(List<UploadFileInfo> listData) {
        this.listData = listData;
        notifyDataSetChanged();
    }

    public void setItemClickListener(MyItemClickListener itemClickListener) {
        this.itemClickListener = itemClickListener;
    }

    @Override
    public UserProfileImgGridViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = View.inflate(parent.getContext(), R.layout.ac_user_profile_gridview_item, null);
        UserProfileImgGridViewHolder viewHolder = new UserProfileImgGridViewHolder(view, itemClickListener);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(UserProfileImgGridViewHolder holder, int position) {
        if (null == listData || listData.size() == 0){
            return;
        }
        UploadFileInfo uploadFileInfo = (UploadFileInfo)listData.get(position);
        String imageUrl = uploadFileInfo.url;
        if (!TextUtils.isEmpty(imageUrl)) {
            holder.userProfilePhoto.setScaleType(ImageView.ScaleType.CENTER_CROP);
            int nwidth = 60;
            BaseImgView.loadimg(holder.userProfilePhoto, ImageUtils.getThumbnailFullPath(imageUrl,
                            nwidth + "x" +
                                    nwidth), R.drawable.ic_pic_loading_default_small,
                    R.drawable.ic_pic_loading_default_small, R.drawable.ic_pic_loading_default_small, ImageScaleType.EXACTLY,
                    nwidth,
                    nwidth, 0);
        } else {
            holder.userProfilePhoto.setImageResource(R.drawable.ic_pic_loading_default_small);
        }
    }

    @Override
    public int getItemCount() {
        return listData != null ? listData.size() : 0;
    }

    class UserProfileImgGridViewHolder extends RecyclerView.ViewHolder {
        ImageView userProfilePhoto;

        public UserProfileImgGridViewHolder(View itemView, final MyItemClickListener itemClickListener) {
            super(itemView);
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (itemClickListener != null) {
                        itemClickListener.OnItemClickListener(v, getPosition());
                    }
                }
            });
            userProfilePhoto = (ImageView)itemView.findViewById(R.id.user_profile_grid_item);
        }
    }

    public interface MyItemClickListener {
        void OnItemClickListener(View v, int position);
    }

}
